package bb.lanxing.lib.kt

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import java.util.concurrent.atomic.AtomicReference


object XossCoroutineScopeKt {
    private val scopeRef = AtomicReference<CoroutineScope>()
    fun getXossCoroutineScope(): CoroutineScope {
        var safeCoroutineScope: SafeCoroutineScope
        do {
            val coroutineScope = scopeRef.get()
            if (coroutineScope != null) {
                return coroutineScope
            }
            safeCoroutineScope = SafeCoroutineScope(Dispatchers.Main)
        } while (!scopeRef.compareAndSet(null, safeCoroutineScope))
        return safeCoroutineScope
    }

}